/**
 * 获取手机验证码 封装
 * 
 * 版本：v1.0
 * 
 * Stanley
 **/
layui.define(['jquery'],
    function (exports) {
        var $ = layui.jquery;
        var kaPhonenVerCode = new function () {
            // 初始化
            this.init = function (phonenVerCodeId, phonenumber, layuiFormId, type) {
                this.loadPhonenVerCodeTime(phonenVerCodeId, phonenumber, layuiFormId);
                this.buttonClick(phonenVerCodeId, phonenumber, layuiFormId, type);
            }
            // 加载 获取手机验证码的按钮事件
            this.loadPhonenVerCodeTime = function (PhonenVerCodeId, phonenumber, layuiFormId) {
                var res = {};
                res["intervalName"] = "phonenVerCodeButton"; // 手機號碼
                res["PhonenVerCodeId"] = PhonenVerCodeId; // 手機號碼
                res["phonenumber"] = phonenumber; // 手機號碼
                res["sessionStorageValue"] = "phonenVerCodePhone"; // sessionStorage Name
                res["sessionStorageTime"] = "phonenVerCodeTime"; // sessionStorage Name
                res["layuiBtnDisabled"] = "layui-btn-disabled";
                res["PhonenVerCodeText"] = "获取";
                $("#" + res.PhonenVerCodeId).html("加载中");
                $("#" + PhonenVerCodeId).addClass(res.layuiBtnDisabled);
                // 加載 kaInterval
                layui.kaInterval.init(res.intervalName, res, function (resData) {
                    // 判斷手機驗證碼
                    var phonenVerCodePhone = sessionStorage.getItem(resData.sessionStorageValue);
                    if (phonenVerCodePhone == undefined || phonenVerCodePhone == null || phonenVerCodePhone == "") {
                        layui.kaInterval.clear(resData.intervalName, resData, function (resDataData) {
                            $("#" + resDataData.PhonenVerCodeId).removeClass(resDataData.layuiBtnDisabled);
                            $("#" + resDataData.PhonenVerCodeId).html(res.PhonenVerCodeText);
                        });
                        return false
                    } else {
                        var valform = {};
                        valform[phonenumber] = phonenVerCodePhone;
                        layui.form.val(layuiFormId, valform);
                    }
                    // 判斷驗證碼時間
                    var phonenVerCodeTime = sessionStorage.getItem(resData.sessionStorageTime);
                    if (phonenVerCodeTime == undefined || phonenVerCodeTime == null || phonenVerCodeTime == "") {
                        layui.kaInterval.clear(resData.intervalName, resData, function (resDataData) {
                            $("#" + resDataData.PhonenVerCodeId).removeClass(resDataData.layuiBtnDisabled);
                            $("#" + resDataData.PhonenVerCodeId).html(resDataData.PhonenVerCodeText);
                        });
                        return false
                    }
                    // 处理业务判断停止
                    layui.kaInterval.judge(60, phonenVerCodeTime, resData, function (resDataData) {
                        layui.kaInterval.clear(resDataData.intervalName, resDataData, function (resDataDataData) {
                            $("#" + resDataDataData.PhonenVerCodeId).removeClass(resDataDataData.layuiBtnDisabled);
                            $("#" + resDataDataData.PhonenVerCodeId).html(resDataDataData.PhonenVerCodeText);
                        });
                    }, function (resData, syxx) {
                        $("#" + resData.PhonenVerCodeId).html(parseInt(syxx));
                    })
                }, 10);
            }
            // 绑定按钮事件
            this.buttonClick = function (phonenVerCodeId, phonenumber, layuiFormId, type) {
                var that = this;
                $("#" + phonenVerCodeId).unbind(); //先取消綁定事件
                $("#" + phonenVerCodeId).click(function () {
                    var loginFormVal = layui.form.val(layuiFormId);
                    if (layui.kaForm.rule.isNull(loginFormVal[phonenumber])) {
                        layui.kaMsg.error("手机号不能为空", JSON.stringify(loginFormVal));
                        return false;
                    }
                    if (!layui.kaForm.rule.isMobile(loginFormVal[phonenumber])) {
                        layui.kaMsg.error("无效的手机号", JSON.stringify(loginFormVal));
                        return false;
                    }
                    loginFormVal["type"] = type;
                    layui.kaAjax.ajax(layui.kaBgUrl.url("getPhonenVerCode"), function (rs) {
                        if (rs.code == 200) {
                            sessionStorage.setItem("phonenVerCodePhone", loginFormVal[phonenumber]);
                            sessionStorage.setItem("phonenVerCodeTime", new Date().getTime());
                            that.loadPhonenVerCodeTime(phonenVerCodeId, phonenumber);
                        } else {
                            layui.kaMsg.error("获取错误" + rs.msg, JSON.stringify(rs));
                        }
                    },loginFormVal)
                });
            }
        };
        exports('kaPhonenVerCode', kaPhonenVerCode);
    }
)